package com.sinog.biz.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 拼接insert工具
 * @author lzq
 * @date 2020-08-31 4:12 下午
 */
@Slf4j
public class CreateInsertSqlUtil {

    /**
     * 生成插入sql语句
     * @param tableName tableName
     * @param dataMap dataMap
     * @param columnsList columnsList
     * @return HashMap<String,Object>
     */
    public static HashMap<String,Object> buildInsertSql1(String tableName,HashMap<String,Object> dataMap,List<String> columnsList) {
        HashMap<String,Object> insertHashMap = new HashMap<>(16);
        StringBuilder sql = new StringBuilder("insert into ").append(tableName).append(" (");
        StringBuilder args = new StringBuilder(16);
        args.append("(");
        List<Object> params = new ArrayList<>(16);
        for(Map.Entry<String,Object> entry : dataMap.entrySet()) {
            if(null != entry.getValue() && !"".equals(entry.getValue())) {
                sql.append(entry.getKey());
                args.append("?");
                params.add(entry.getValue());
                sql.append(",");
                args.append(",");
            }
        }
        args.deleteCharAt(args.length() - 1);
        sql.deleteCharAt(sql.length() - 1);
        args.append(")");
        sql.append(")");
        sql.append(" values ");
        sql.append(args);
        insertHashMap.put("sql",sql);
        insertHashMap.put("params",params);
        return insertHashMap;
    }

    /**
     * 生成插入sql语句
     * @param tableName tableName
     * @param dataMap dataMap
     * @param columnsList columnsList
     * @return HashMap<String,Object>
     */
    public static HashMap<String,Object> buildInsertSql2(String tableName,HashMap<String,Object> dataMap,List<String> columnsList) {
        HashMap<String,Object> insertHashMap = new HashMap<>(16);
        StringBuilder sql = new StringBuilder("insert into ").append(tableName).append(" (");
        StringBuilder args = new StringBuilder(16);
        args.append("(");
        List<Object> params = new ArrayList<>(16);
        for(String columnBu : columnsList) {
            for(Map.Entry<String,Object> entry : dataMap.entrySet()) {
                String cloumnName = entry.getKey();
                boolean flag = null != entry.getValue() && !"".equals(entry.getValue()) && (cloumnName.equalsIgnoreCase(getUnderlineName(columnBu)) || columnBu.equalsIgnoreCase(getUnderlineName(cloumnName)));
                if(flag) {
                    sql.append(columnBu);
                    args.append("?");
                    params.add(entry.getValue());
                    sql.append(",");
                    args.append(",");
                }
            }
        }
        args.deleteCharAt(args.length() - 1);
        sql.deleteCharAt(sql.length() - 1);
        args.append(")");
        sql.append(")");
        sql.append(" values ");
        sql.append(args);
        insertHashMap.put("sql",sql);
        insertHashMap.put("params",params);
        return insertHashMap;
    }

    /**
     * 生成插入sql语句
     * @param dataMap dataMap
     * @param columnsList columnsList
     * @return Object[]
     */
    public static Object[] buildInsertSql3(HashMap<String,Object> dataMap,List<String> columnsList) {
        Object[] obj = new Object[columnsList.size()];
        for(int i = 0; i < columnsList.size(); i++) {
            String columnBu = columnsList.get(i);
            for(Map.Entry<String,Object> entry : dataMap.entrySet()) {
                String columnName = entry.getKey();
                boolean flag = null != entry.getValue() && !"".equals(entry.getValue()) && (columnName.equalsIgnoreCase(getUnderlineName(columnBu)) || columnBu.equalsIgnoreCase(getUnderlineName(columnName)));
                if(flag) {
                    obj[i] = entry.getValue();
                }
            }
            if(null == obj[i]) {
                obj[i] = null;
            }
        }
        return obj;
    }

    /**
     * 描述
     * @param propertyName propertyName
     * @return String
     */
    private static String getUnderlineName(String propertyName) {
        if(null == propertyName) {
            return "";
        }
        StringBuilder sbl = new StringBuilder(propertyName);
        sbl.setCharAt(0,Character.toLowerCase(sbl.charAt(0)));
        propertyName = sbl.toString();
        char[] chars = propertyName.toCharArray();
        StringBuilder sb = new StringBuilder(16);
        for(char c : chars) {
            if(CharUtils.isAsciiAlphaUpper(c)) {
                sb.append("_").append(StringUtils.lowerCase(CharUtils.toString(c)));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}